package com.isoftstone.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoftstone.wms.common.QueryPageParam;
import com.isoftstone.wms.common.Result;
import com.isoftstone.wms.entity.Menu;
import com.isoftstone.wms.entity.User;
import com.isoftstone.wms.service.impl.MenuService;
import com.isoftstone.wms.service.impl.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2025-09-30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private MenuService menuService;

    @GetMapping("/list")
    public List<User> list() {
        return userService.list();
    }
    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        List list =userService.lambdaQuery()
                .eq(User::getNo, user.getNo())
                .eq(User::getPassword, user.getPassword()).list();
        if (list.size() > 0) {
            User user1 = (User) list.get(0);
            // 修改权限查询逻辑，支持多权限匹配
            String roleId = user1.getRoleId().toString();
            List menuList = menuService.lambdaQuery()
                    .apply("FIND_IN_SET({0}, menuRight)", roleId)
                    .list();
            HashMap res=new HashMap();
            res.put("user", user1);
            res.put("menuList", menuList);
            return Result.success(res);
        }

        return Result.fail();
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user)?Result.success() : Result.fail();
    }

    //修改
    @PostMapping("/mod")
    public Result mod(@RequestBody User user) {
        return userService.updateById(user) ? Result.success() : Result.fail();
    }

    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }

    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id) {
        return userService.removeById(id);
    }

    //查询(模糊/匹配)
    @PostMapping("/listp")
    public Result listp(@RequestBody User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(user.getName())){
            wrapper.like(User::getName, user.getName());
        }


        return Result.success(userService.list(wrapper));

    }

    @PostMapping("/listpage")
//    public List<User> listpage(@RequestBody HashMap map) {
    public List<User> listpage(@RequestBody QueryPageParam query) {
        // 获取前端传过来的查询参数
        HashMap<String, Object> param = query.getParam();
        String name = (String) param.get("name");
        // 1. 创建分页对象
        Page<User> page = new Page<>();
        page.setCurrent(query.getPageNum()); // 当前页码
        page.setSize(query.getPageSize());  // 每页条数
        // 2. 创建条件构造器
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getName, name); // 模糊查询
        // 3. 调用分页查询
        IPage<User> result = userService.page(page, wrapper);
        return result.getRecords();
    }

    @PostMapping("/listpageC")
    public Result listpageC(@RequestBody QueryPageParam query) {
        // 获取前端传过来的查询参数
        HashMap<String, Object> param = query.getParam();
        String name = (String) param.get("name");
        // 获取性别参数，兼容字符串和数字类型
        Integer sex = null;
        Object sexObj = param.get("sex");
        if (sexObj instanceof Integer) {
            sex = (Integer) sexObj;
        } else if (sexObj instanceof String) {
            String sexStr = (String) sexObj;
            if (StringUtils.isNotBlank(sexStr)) {
                try {
                    sex = Integer.valueOf(sexStr);
                } catch (NumberFormatException ignored) {
                }
            }
        }
        
        // 获取角色ID参数，兼容字符串和数字类型
        Integer roleId = null;
        Object roleIdObj = param.get("roleId");
        if (roleIdObj instanceof Integer) {
            roleId = (Integer) roleIdObj;
        } else if (roleIdObj instanceof String) {
            String roleIdStr = (String) roleIdObj;
            if (StringUtils.isNotBlank(roleIdStr)) {
                try {
                    roleId = Integer.valueOf(roleIdStr);
                } catch (NumberFormatException ignored) {
                }
            }
        }
        // 1. 创建分页对象
        Page<User> page = new Page<>();
        page.setCurrent(query.getPageNum()); // 当前页码
        page.setSize(query.getPageSize());  // 每页条数
        // 2. 创建条件构造器
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            wrapper.like(User::getName, name);
        }
        if(sex != null){
            wrapper.eq(User::getSex, sex);
        }
        if(roleId != null){
            wrapper.eq(User::getRoleId, roleId);
        }
       // 模糊查询
        // 3. 调用分页查询
        IPage<User> result = userService.page(page, wrapper);
        return Result.success(result.getRecords(), result.getTotal());


    }

    // 检查账号是否重复
    @PostMapping("/checkAccount")
    public Result checkAccount(@RequestBody HashMap<String, String> param) {
        String no = param.get("no");
        if (StringUtils.isBlank(no)) {
            return Result.fail("账号不能为空");
        }
        
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getNo, no);
        User existUser = userService.getOne(wrapper);
        
        if (existUser != null) {
            return Result.fail("账号已存在");
        } else {
            return Result.success("账号可用");
        }
    }


}
